A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).
def find_perfect_numbers(start, end):
perfect_numbers = []
for num in range(start, end + 1):
divisors_sum = sum([divisor for divisor in range(1, num) if num % divisor == 0])
if divisors_sum == num:
perfect_numbers.append(num)
return perfect_numbers
def find_and_display_perfect_numbers():
start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
perfect_numbers = find_perfect_numbers(start, end)
print("Perfect numbers in the range", start, "to", end, "are:", perfect_numbers)
find_and_display_perfect_numbers()
Enter the start of the range: 1
Enter the end of the range: 10000
Perfect numbers in the range 1 to 10000 are: [6, 28, 496, 8128]
Enter the start of the range: 1
Enter the end of the range: 1000
Perfect numbers in the range 1 to 1000 are: [6, 28, 496]
The function find_perfect_numbers(start, end)
finds all the perfect numbers within the given range by checking each number's sum of proper divisors.
The function find_and_display_perfect_numbers()
takes input for the range, calls the first function, and displays the list of perfect numbers found.